<template>
  <div class="test-answer">
    <Header />
    
    <div class="answer-content">
      <h2>{{ title }}</h2>
      
      <div class="questions" v-loading="loading">
        <template v-if="questions.length > 0">
          <div v-for="question in questions" 
            :key="question.questionNum" 
            class="question-item"
          >
            <h3>{{ question.questionNum }}. {{ question.text }}</h3>
            <el-radio-group v-model="answers[question.questionNum - 1]">
              <el-radio :label="1">A. {{ question.optionText1 }}</el-radio>
              <el-radio :label="2">B. {{ question.optionText2 }}</el-radio>
              <el-radio :label="3">C. {{ question.optionText3 }}</el-radio>
              <el-radio :label="4">D. {{ question.optionText4 }}</el-radio>
            </el-radio-group>
          </div>

          <div class="action-buttons">
            <el-button type="primary" @click="submitAnswers">
              提交答案
            </el-button>
          </div>
        </template>
        
        <el-empty v-else description="暂无问题" />
      </div>
    </div>

    <!-- 得分结果对话框 -->
    <el-dialog
      v-model="scoreDialogVisible"
      title="测试结果"
      width="30%"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="score-content">
        <h3>您的总得分是：</h3>
        <div class="total-score">{{ totalScore }}</div>
      </div>
      <template #footer>
        <el-button type="primary" @click="handleClose">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import Header from '../../components/header.vue'
import { getQuestionaireQuestions } from '../../api/test'

interface Question {
  questionId: number
  questionNum: number
  text: string
  optionText1: string
  optionText2: string
  optionText3: string
  optionText4: string
  questionaireId: number
}

const route = useRoute()
const router = useRouter()
const loading = ref(false)
const title = ref(route.query.title as string || '问卷答题')
const questions = ref<Question[]>([])
const answers = ref<number[]>([])
const scoreDialogVisible = ref(false)
const totalScore = ref(0)

// 获取问卷问题
const fetchQuestions = async () => {
  loading.value = true
  try {
    const res = await getQuestionaireQuestions(Number(route.params.id))
    if (res.code === '200') {
      questions.value = res.data
      // 初始化答案数组，使用问题数量
      answers.value = new Array(res.data.length).fill(null)
    } else {
      ElMessage.error(res.msg || '获取问题失败')
    }
  } catch (error) {
    console.error('获取问题失败:', error)
    ElMessage.error('获取问题失败')
  } finally {
    loading.value = false
  }
}

// 提交答案
const submitAnswers = async () => {
  // 检查是否所有问题都已回答
  if (answers.value.some(answer => answer === null)) {
    ElMessage.warning('请回答所有问题')
    return
  }

  // 计算总分
  totalScore.value = answers.value.reduce((sum, answer) => sum + answer, 0)
  
  // 显示得分对话框
  scoreDialogVisible.value = true
}

// 关闭得分对话框
const handleClose = () => {
  scoreDialogVisible.value = false
  // 返回问卷列表页
  router.push('/test')
}

onMounted(() => {
  fetchQuestions()
})
</script>

<style scoped>
.test-answer {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.answer-content {
  flex: 1;
  width: 800px; /* 固定宽度为800px */
  margin: 84px auto 40px; /* 居中显示 */
  padding: 20px;
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-size: 24px; /* 增加题目题干的字体大小 */
}

.question-item {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.question-item h3 {
  margin: 0 0 16px;
  color: #2c3e50;
  font-size: 16px;
  padding-left: 16px;
  border-left: 4px solid #409EFF;
  margin-left: 20px;
}

/* 假设选项是以label包含input和span的形式出现 */
.question-item label {
  text-align: left; /* 选项居左显示 */
  margin-bottom: 10px; /* 为选项增加一些间距 */
}

.question-item label input {
  margin-right: 8px; /* 调整输入框与文本之间的间距 */
}

/* 如果选项是列表形式，例如ul/li */
.question-item ul {
  padding-left: 0; /* 移除默认的padding */
  list-style: none; /* 移除列表点 */
}

.question-item li {
  text-align: left; /* 选项居左显示 */
  margin-bottom: 10px; /* 为选项增加一些间距 */
}
.el-radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px 0;
  margin-left: 20px;
}

:deep(.el-radio) {
  margin-left: 0 !important;  /* 覆盖 Element Plus 的默认样式 */
  padding: 8px 16px;
  width: 100%;
  display: flex;
  align-items: center;
}

:deep(.el-radio__label) {
  font-size: 16px;
  color: #333;
  padding-left: 8px;
}

:deep(.el-radio__input) {
  height: 14px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

.score-content {
  text-align: center;
}

.total-score {
  font-size: 48px;
  color: #409EFF;
  margin: 20px 0;
  font-weight: bold;
}
</style> 